MyDesktop.BrandWindow = Ext.extend(Ext.app.Module, {
    id:'brand-win',
    init : function(){
        this.launcher = {
            text: '品牌管理',
            iconCls:'icon-grid',
            handler : this.createWindow,
            scope: this
        }
    },
    bugRecord : Ext.data.Record.create([
	    {name : 'id',mapping : 'id',type : 'int'}, 
	    {name : 'name',mapping : 'name',type : 'string'}, 
	    {name : 'pinyin',mapping : 'pinyin',type : 'string'}

	   ]),
	cm_bug : new Ext.grid.ColumnModel([
	       new Ext.grid.RowNumberer(),
		   {header : '品牌名称', width : 120,dataIndex : 'name'},
		   {header : '简拼',width : 120,dataIndex : 'pinyin'}
	 ]),
	 
	 getPanel: function(){
	 	var store=new Ext.data.Store({
			  url : app.url+'/admin/sys/barndList.do',
		      baseParams:{start:0,limit:20},
		      autoLoad : true,
              reader: new Ext.data.JsonReader({totalProperty : 'totalProperty',root : 'root'}, this.bugRecord)
         });
         
         var bug_gridPanel =  new Ext.grid.GridPanel({
                        border:false,
						columnLines :true,
                        enableHdMenu : false,
						loadMask : {msg : '数据加载中...'},
                        ds: store,
                        cm: this.cm_bug,
                        columnLines :true,
                        enableHdMenu : false,
                        viewConfig: {
                            forceFit:true
                        },
                        tbar:[{
                            text:'增加',
                            tooltip:'增加故障信息',
                            iconCls:'add',
                            handler:addBug
                        }, '-', {
                            text:'修改',
                            tooltip:'编辑故障信息',
                            iconCls:'option',
                            handler:editBug
                        },'-',{
                            text:'删除',
                            tooltip:'Remove the selected item',
                            iconCls:'remove'
                        }],
						bbar: new Ext.PagingToolbar({
                            pageSize: 20,
                            store: store,
                            displayInfo: true,
                            plugins: new Ext.ux.ProgressBarPager()
                        })
              });
              
               var bug_detailForm = new Ext.FormPanel({
       		 	 	labelSeparator : "：",
				 	frame:true,
				 	border:false,
				 	defaultType : 'textfield',
				 	defaults:{width:160},
				 	items:[{
			              xtype:'hidden',
			              name:'id'			              			
			            },{				     	
				      	 fieldLabel:'品牌名称',
				      	 name:"name",
						 allowBlank : false,
						 minLength : 2,
			             maxLength : 20
				     	}],
				 	buttons:[
			       		{
			     		  id:'commitButton',
						  iconCls : 'save',
			        	  handler:submitForm
			       		},{
			     	      id:'closeButton',
						  text : '关闭',
						  iconCls : 'close',
			     	      handler:function(){				     		
			     	           addOrEditBug_win.hide();
			     	      }
			          }
			     ]
       		 });
       		 
       		 var addOrEditBug_win = new Ext.Window({
       		 	layout:'fit',
		    	width:330,
		    	closeAction:'hide',
		   		height:130,
				resizable :false,
				shadow : true,
				modal :true,
		   	    closable:true,
		    	bodyStyle:'padding:5 5 5 5',		    	
		    	animCollapse:true,
				items:[bug_detailForm]
       		 });
       		 
       		      //取得选中的记录
	 		function getSelectedItem(){
	     		var record= bug_gridPanel.getSelectionModel().getSelected();	   
	     		if(record == null){
	        		 Ext.MessageBox.alert('提示','请选择要修改的明细！');
	         		return;
	     		}else{
	        		return record;
	     		}
	  		}
            
            function addBug(){
            	if(bug_detailForm.form.el){
            		bug_detailForm.form.el.dom.reset();            	
            	}else{
            		bug_detailForm.form.reset();
            	}
                Ext.getCmp('commitButton').setText('新增');
		        addOrEditBug_win.isAdd = true;
		        addOrEditBug_win.setTitle("新增品牌信息");
		        addOrEditBug_win.show();
            }
            
            function editBug(){
            	if(bug_detailForm.form.el){
            		bug_detailForm.form.el.dom.reset();            	
            	}else{
            		bug_detailForm.form.reset();
            	}
            	var record=getSelectedItem();
            	if(record){
            		Ext.getCmp('commitButton').setText('修改');
			    	addOrEditBug_win.setTitle("编辑品牌");
			    	addOrEditBug_win.isAdd = false;
			    	bug_detailForm.getForm().loadRecord(record);
			    	addOrEditBug_win.show();
            	}
            	
            }

            function submitForm(){
            	if(!bug_detailForm.getForm().isValid()){
	         		 return;
	      		}
	      		if(addOrEditBug_win.isAdd){
	      		   	bug_detailForm.getForm().submit({
	           			url:app.url+'/admin/sys/createBrand.do',
	           			method :"post",
						waitMsg : '正在提交数据,请稍候...',
	          			success:function(response,options){
							 store.reload();
							 Ext.MessageBox.alert("提示","操作成功!");
							 addOrEditBug_win.hide();
	           			},
	           			failure:function(response,options){
	                		Ext.MessageBox.alert("提示","发生异常，请稍候处理!");    
	           			}
	       			});
	      		}else{
            		bug_detailForm.getForm().submit({
	           			url:app.url+'/admin/sys/updateBrand.do',
	           			method :"post",
						waitMsg : '正在提交数据,请稍候...',
	          			success:function(response,options){
							store.reload();
							Ext.MessageBox.alert("提示","修改成功!");
							addOrEditBug_win.hide();
	           			},
	           			failure:function(response,options){
	                		Ext.MessageBox.alert("提示","发生异常，请稍候处理!");    
	           			}
	       			});
           		}
         	}
         	
         	return bug_gridPanel;
	 
	 },

    createWindow : function(){
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('brand-win');
        if(!win){
	
            win = desktop.createWindow({
                id: 'brand-win',
                title:'品牌管理',
                width:app.minWidth,
                height:app.minHeight,
                iconCls: 'icon-grid',
                shim:false,
                animCollapse:false,
                constrainHeader:true,
                layout: 'fit',
                items:this.getPanel()
                   
            });
        }
        win.show();
    }
});